package com.ptswitch.material.pages.contract;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.hxzon.util.BigDecimalUtil;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractPayment;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractPaymentService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.MaterialUtil;

public class ReplyRefund extends MaterialBasePage {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractPaymentService contractPaymentSer;
    @Property
    private ContractPayment paymentAsker;
    @Property
    private ContractPayment payment;
    private Contract contract;
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String id) {
        paymentAsker = contractPaymentSer.findById(id);
        contract = paymentAsker.getContract();
        checkUserIsEqual(contract.getFinancial());
        payment = new ContractPayment();
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return paymentAsker.getId();
    }

    @OnEvent(value = EventConstants.PREPARE)
    void prepareValidateRestrict() {
        payment.setPaymentCurrent(paymentAsker.getPayment());
        payment.setPayment(payment.getPaymentCurrent());
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "replyPayment")
    Object addPayment() {
        checkUserIsEqual(contract.getFinancial());
        //check payment
        if (BigDecimalUtil.biggerThan(payment.getPayment(), paymentAsker.getPayment())) {
            throw AppRuntimeException.error("退款金额大于申请金额");
        }
        //save
        payment.setAsker(paymentAsker);
        payment.setRefund(paymentAsker.isRefund());
        payment.setReciever(paymentAsker.getSender());
        payment.setSender(getVisitor());
        payment.setContract(contract);
        payment.setPaymentType(ContractPayment.Reply_Payment);
        contractPaymentSer.save(payment);
        //update asker
        paymentAsker.setAsker(payment);
        contractPaymentSer.save(paymentAsker);
        //update contract account paid
        contract = contractSer.findById(contract.getId());
        contract.updateForAddPayment(payment);
        contractSer.save(contract);
        //send email
        EMail email = getMaterialUtil().newEMail();
        email.setSubject(payment.getSender().getRealname() + "回复了你的退款申请");
        email.addTo(payment.getReciever().getEmail());
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        email.addText("回复退款金额：" + payment.getPayment());
        email.addText("回复退款备注：" + payment.getRemark());
        EMailUtil.send(email);
        return viewContract.initialize(contract.getId());
    }
}
